<?php

class Shifts Extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library(array('form_validation'));
		
	
	}
	
	
	public function add()
	{
		$this->load->model('M_shifts','ms');
		$this->view_data['system_message'] = $this->_msg();
		$this->view_data['shifts'] = $this->ms->get_time_shift();
		
		if($this->input->post('new_time_shifts'))
		{
			$this->form_validation->set_rules('timein','Time In','required|trim|htmlspecialchars');
			$this->form_validation->set_rules('timeout','Time Out','required|trim|htmlspecialchars');
			
			if($this->form_validation->run() !== FALSE)
			{
				$q = (object)$this->input->post();
				$this->load->model('M_shifts','ms');
				if($this->ms->new_time_shifts($q))
				{
					$this->_msg('s','Shift was successfully added','shifts/add');
				}else{
					$this->_msg('e','unable to add shifts','shifts/add');
				}
			}else{
				$this->view_data['system_message'] = validation_errors() == '' ? NULL : $this->_msg('e',validation_errors());
			}
			
		
		}
	}
	
	public function view()
	{
		$this->load->model('M_shifts','ms');
		$this->view_data['shifts'] = $this->ms->get_time_shift();
		
	}
	
	public function update()
	{
	
	
	}
	
	public function delete()
	{
	
	
	}
	
	public function department()
	{
		$this->view_data['system_message'] = $this->_msg();
		$this->load->model('M_department','md');
		$this->load->model('M_shifts','ms');
		$this->load->library('pagination');
		
		$config['base_url'] = base_url().'department/view';
		$config['total_rows'] = $this->md->count_dept();
		$config['per_page'] = 10; 
		
		//Pagnate Design start
			$config['full_tag_open'] = '<div class="pagination pagination-centered"><ul>';
			$config['full_tag_close'] = '</ul></div>';
			
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			
			$config['cur_tag_open'] = '<li class="active"><a href="#">';
			$config['cur_tag_close'] = '</a></li>';
			
			$config['prev_link'] = '&lt;';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			
			$config['next_link'] = '&gt;';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			
			$config['first_link'] = 'First';
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			
			$config['last_link'] = 'Last';
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			
		//Pagnate Design end
		
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$this->pagination->initialize($config); 
		$this->view_data['departments'] = $this->md->fetch_dept($config['per_page'] ,$page);
		$this->view_data['pagnate_links'] = $this->pagination->create_links();
		$this->view_data['employees'] = function($x){return $this->_get_employee_by_dept($x);};
	
	}
	

	
	public function update_department($x)
	{
		if(is_numeric($x))
		{
			$this->load->model('M_shifts','ms');	
			$this->view_data['time_shifts'] = $this->ms->get_time_shift_array();
			$this->view_data['system_message']= $this->_msg();
			$this->view_data['employees_on_dept'] = $this->ms->get_employee_time_shifts_by_dept($x);
			$this->view_data['id'] = $x;
			
			if($this->input->post('save_shifts'))
			{
				$posts = $this->input->post('shift');
				$count = 0;
				foreach($posts as $k => $v)
				{
					$input[$count]['emp_id'] = $k;
					$input[$count]['shift_id'] = $v;
					$input[$count]['created'] = NOW;
					$input[$count]['dep_id'] = $x;
					$input[$count]['active'] = 1;
					$count++;
				}
				

				if($this->ms->add_employee_shifts($input,$x))
				{
					$this->_msg('s','Shifts Saved','departmentshiftsupdate/'.$x);
				}else{
					$this->_msg('e','Encountered an error while saving shifts','departmentshiftsupdate/'.$x);
				}
			}
		}
	}
			
	
	private function _get_employee_by_dept($id)
	{
		$this->load->model('M_employee','me');
		$this->load->model('M_shifts','ms');
		return $this->ms->get_employee_and_shifts($id);
	}

}